package com.maotai.llzhou.servlet;

import java.net.URLDecoder;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.maotai.llzhou.service.HuaRunService;


@Controller 
public class HuaRunController {
	@Resource
	private HuaRunService huaRunService;    
	@ResponseBody
	@RequestMapping(value="/getHROrder",produces = {"application/json;charset=UTF-8"}) 
	public String getHROrder()
	{
		String returtnstr = "";
    	try {
			Map<String, Object> orderMap = huaRunService.getHROrder();
			if(orderMap!=null && orderMap.size()>0){
				returtnstr = JSONObject.toJSONString(orderMap);
			}
    	} catch (SQLException e) {
			e.printStackTrace();
		}
    	System.out.println(returtnstr);
    	return returtnstr;
	}
	@ResponseBody
	@RequestMapping(value="/getZhanghaoInfo",produces = {"application/json;charset=UTF-8"}) 
	public String getZhanghaoInfo()
	{
		String returtnstr = "";
    	try {
    		List<Map<String, Object>> list = huaRunService.getZhanghaoInfo();
			if(list!=null && list.size()>0){
				JSONArray jArr = JSONArray.fromObject(list);
				returtnstr = jArr.toString();
			}
    	} catch (SQLException e) {
			e.printStackTrace();
		}
    	return returtnstr;
	}
	@ResponseBody
	@RequestMapping(value="/getUniqueId",method = RequestMethod.GET,produces = {"application/text;charset=UTF-8"}) 
	public String getUniqueId(HttpServletRequest request)
	{
		String returtnstr = "";
		String telphone=request.getParameter("telphone");
			Map<String,String> map = new HashMap<String,String>();
				map.put("telphone", telphone);
			try {
				Map<String, Object> orderMap = huaRunService.getUniqueId(map);
				if(orderMap!=null && orderMap.size()>0){
					returtnstr = JSONObject.toJSONString(orderMap);
				}
	    	} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	return returtnstr;
	}
	@ResponseBody
	@RequestMapping(value="/getAppVersion",method = RequestMethod.GET,produces = {"application/text;charset=UTF-8"}) 
	public String getAppVersion(HttpServletRequest request)
	{
		String returtnstr = "";
			try {
				Map<String, Object> orderMap = huaRunService.getAppVersion();
				if(orderMap!=null && orderMap.size()>0){
					returtnstr = JSONObject.toJSONString(orderMap);
				}
	    	} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	return returtnstr;
	}
	@ResponseBody
	@RequestMapping(value="/updateHROrder",produces = {"application/text;charset=UTF-8"}) 
	public int updateHROrder(@RequestBody String param)
	{
		int cou = 0;
		System.out.println(param);
		String[] paramStrs = param.split("&");
		if(paramStrs.length>0){
			Map<String,String> map = new HashMap<String,String>();
			for(String temp : paramStrs ){
				String[] temps = temp.split("=");
				map.put(temps[0], temps[1]);
			}
			try {
				cou = huaRunService.updateHROrder(map);
	    	} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
    	return cou;
	}
	@ResponseBody
	@RequestMapping(value="/insertHROrder",produces = {"application/text;charset=UTF-8"}) 
	public int insertHROrder(@RequestBody String param)
	{
		int cou = 0;
		try {
			param = URLDecoder.decode(param,"utf-8");
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		System.out.println(param);
		String[] paramStrs = param.split("&");
		if(paramStrs.length>0){
			Map<String,String> map = new HashMap<String,String>();
			for(String temp : paramStrs ){
				String[] temps = temp.split("=");
				map.put(temps[0], temps[1]);
			}
			try {
				cou = huaRunService.insertHROrder(map);
	    	} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
    	return cou;
	}
	@ResponseBody
	@RequestMapping(value="/insertQQOrder",produces = {"application/json;charset=UTF-8"}) 
	public int addcode(@RequestBody String param)
	{
		int cou = 0;
		System.out.println(param);
		String[] paramStrs = param.split("&");
		if(paramStrs.length>0){
			Map<String,String> map = new HashMap<String,String>();
			for(String temp : paramStrs ){
				String[] temps = temp.split("=");
				if("randstr".equals(temps[0])){
					temps[1] = temps[1].replace("%40", "@");
				}
				map.put(temps[0], temps[1]);
			}
			try {
				cou = huaRunService.insertQQOrder(map);
	    	} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
    	return cou;
	}
	@ResponseBody
	@RequestMapping(value="/queryQQOrder",produces = {"application/json;charset=UTF-8"}) 
	public String queryQQOrder(){
		String returtnstr = "{}";
    	try {
			List<Map<String, Object>> list = huaRunService.queryQQOrder();
			if(list!=null && list.size()>0){
				JSONArray jArr = JSONArray.fromObject(list);
				returtnstr = jArr.toString();
			}
    	} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return returtnstr;
	}
	@ResponseBody
	@RequestMapping(value="/queryOneQQOrder",produces = {"application/json;charset=UTF-8"}) 
	public String queryOneQQOrder(){
		String returtnstr = "{}";
    	try {
			List<Map<String, Object>> list = huaRunService.queryOneQQOrder();
			if(list!=null && list.size()>0){
				JSONArray jArr = JSONArray.fromObject(list);
				returtnstr = jArr.toString();
			}
    	} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return returtnstr;
	}
	
	@ResponseBody
	@RequestMapping(value="/uploadUser",produces = {"application/json;charset=UTF-8"}) 
	public String uploadUser(@RequestBody String param) throws Exception{
		System.out.println(param);
    	String returtnstr = "1";
    	String[] dates = param.split("&");
    	String storeCode = dates[0].split("=")[1];
    	String tbText = dates[1].split("=")[1];
    	storeCode = URLDecoder.decode(storeCode,"utf-8");
    	tbText = URLDecoder.decode(tbText,"utf-8");
    	String[] tbTexts = tbText.split("\\n");
    	List<Map<String,String>> userList = new ArrayList<Map<String,String>>();
    	if(tbTexts!=null && tbTexts.length>0){
    		for(String text : tbTexts){
    			Map<String,String> userMap = new HashMap<String,String>();
    			userMap.put("codes", storeCode);
    			String[] texts = text.split(",");
    			if(texts!=null && texts.length>0){
    				userMap.put("ownerId", texts[0]);
    				userMap.put("ownerName", texts[1]);
    				userMap.put("createdate", texts[2]);
    				userMap.put("telphone", texts[3]);
    				userMap.put("password", texts[4]);
    			}
    			userList.add(userMap);
    		}
    	}
    	huaRunService.uploadUser(userList);
    	return returtnstr;
	}
	@ResponseBody
	@RequestMapping(value="/deleteUser",produces = {"application/json;charset=UTF-8"}) 
	public String deleteUser(@RequestBody String param) throws Exception{
		System.out.println(param);
    	String returtnstr = "1";
    	String[] dates = param.split("&");
    	String telphones = dates[0].split("=")[1];
    	Map<String,String> map = new HashMap<String,String>();
    	telphones = URLDecoder.decode(telphones,"utf-8");
    	telphones = telphones.substring(0,telphones.length()-1);
    	map.put("telphones", telphones);
    	huaRunService.deleteUser(map);
    	return returtnstr;
	}
	@ResponseBody
	@RequestMapping(value="/getAllCity",produces = {"application/text;charset=UTF-8"}) 
	public String getAllCity(HttpServletRequest request)
	{	
		System.out.println(request.getParameter("value"));
		String returtnstr = "{}";
    	try {
			List<Map<String, Object>> list = huaRunService.getAllCity();
			if(list!=null && list.size()>0){
				JSONArray jArr = JSONArray.fromObject(list);
				returtnstr = jArr.toString();
			}
    	} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//    	System.out.println(returtnstr);
    	return returtnstr;
	}
	@ResponseBody
	@RequestMapping(value="/queryMaotaiUser",produces = {"application/json;charset=UTF-8"}) 
	public String queryMaotaiUser(@RequestBody String param){
		System.out.println(param);
    	String returtnstr = "[]";
    	String[] dates = param.split("&");
    	String ownerId = dates[0].split("=")[1];
    	Map<String,String> map = new HashMap<String,String>();
    	map.put("ownerId", ownerId);
    	try {
			List<Map<String, Object>> list = huaRunService.queryMaotaiUser(map);
			if(list!=null && list.size()>0){
				JSONArray jArr = JSONArray.fromObject(list);
				returtnstr = jArr.toString();
			}
    	} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return returtnstr;
	}
	@ResponseBody
	@RequestMapping(value="/queryHuaRunOrderSimple",produces = {"application/json;charset=UTF-8"}) 
	public String queryHuaRunOrderSimple(@RequestBody String param){
		System.out.println(param);
    	String returtnstr = "{}";
    	String[] dates = param.split("&");
    	String startDate = dates[0].split("=")[1];
    	String userId = dates[1].split("=")[1];
    	Map<String,String> map = new HashMap<String,String>();
    	map.put("startDate", startDate);
    	map.put("userId", userId);
    	try {
			List<Map<String, Object>> list = huaRunService.queryHuaRunOrderSimple(map);
			if(list!=null && list.size()>0){
				JSONArray jArr = JSONArray.fromObject(list);
				returtnstr = jArr.toString();
			}
    	} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return returtnstr;
	}
	@ResponseBody
	@RequestMapping(value="/queryHuaRunOrderItem",produces = {"application/json;charset=UTF-8"}) 
	public String queryHuaRunOrderItem(@RequestBody String param){
		System.out.println(param);
    	String returtnstr = "{}";
    	String[] dates = param.split("&");
    	String startDate = dates[0].split("=")[1];
    	String userId = dates[1].split("=")[1];
    	Map<String,String> map = new HashMap<String,String>();
    	map.put("startDate", startDate);
    	map.put("userId", userId);
    	try {
			List<Map<String, Object>> list = huaRunService.queryHuaRunOrderItem(map);
			if(list!=null && list.size()>0){
				JSONArray jArr = JSONArray.fromObject(list);
				returtnstr = jArr.toString();
			}
    	} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return returtnstr;
	}
	
	@ResponseBody
	@RequestMapping(value="/updateUserOrderCheck",produces = {"application/text;charset=UTF-8"}) 
	public int updateUserOrderCheck(@RequestBody String param)
	{
		int cou = 0;
		System.out.println(param);
		String[] paramStrs = param.split("&");
		if(paramStrs.length>0){
			Map<String,String> map = new HashMap<String,String>();
			for(String temp : paramStrs ){
				String[] temps = temp.split("=");
				map.put(temps[0], temps[1]);
			}
			try {
				cou = huaRunService.updateUserOrderCheck(map);
	    	} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
    	return cou;
	}
}
